package cn.frh.pet.service.impl;

import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.basic.util.LoginContext;
import cn.frh.pet.domain.Pet;
import cn.frh.pet.domain.PetDetail;
import cn.frh.pet.mapper.PetDetailMapper;
import cn.frh.pet.mapper.PetMapper;
import cn.frh.pet.service.IPetService;
import cn.frh.user.domain.Logininfo;
import cn.frh.user.domain.User;
import cn.frh.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(Pet pet) {
        //先保存pet信息
        petMapper.save(pet);
        //再保存宠物详情信息
        PetDetail detai = pet.getDetail();
        if (detai!=null){
            detai.setPet_id(pet.getId());
            petDetailMapper.save(detai);
        }
    }

    @Override
    public void onSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("onSaleTime",new Date());
        petMapper.onSale(map);
    }

    @Override
    public void offSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        petMapper.offSale(map);
    }

    @Override
    public Pet loadByIdDetail(Long id) {
        return petMapper.loadByIdDetail(id);
    }

    @Override
    public void adopt(Long id,Logininfo logininfo) {
        Pet pet = petMapper.loadById(id);
        //1.下架宠物
        //设置状态
        pet.setState(0);
        //设置下架时间
        pet.setOffsaletime(new Date());
        //2.绑定宠物领养人
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        pet.setUser_id(user.getId());
        petMapper.update(pet);
        //3生成领养订单
    }
}
